import { getUserInfoById } from '@/api/user';
import { ResultCodeEnum } from '@/enums';
import { useUserStore } from '@/store';
import { useToast } from 'wot-design-uni';

export const useCenter = () => {
  const goLogin = () => {
    uni.navigateTo({
      url: useUserStore().isLogined
        ? '/pages/center/register/index?type=edit'
        : '/pages/center/login/index'
    });
  };

  const logout = () => {
    useUserStore().clearUserInfo();
  };

  const { success } = useToast();

  const getUserInfo = async () => {
    const res = await getUserInfoById(useUserStore().userInfo.id);
    if (res.code === ResultCodeEnum.SUCCESS) {
      success({
        msg: '刷新用户信息成功'
      });
      useUserStore().setUserInfo({
        ...res.data,
        accessToken: useUserStore().userInfo.accessToken,
        refreshToken: useUserStore().userInfo.refreshToken
      });
    }
  };
  return {
    goLogin,
    logout,
    getUserInfo
  };
};
